# -*- coding: utf-8 -*-
from bson import json_util
from flask import Flask, jsonify
from flask import abort
from flask import json
from flask import make_response
from flask import request
from pymongo import MongoClient

app = Flask(__name__)
connection = MongoClient("localhost", 27017)
db = connection["douban"]
dylol = db["dylol"]


@app.route('/content/kandian',methods=['GET'])
def get_kandian():
    cur = dylol.find({"label":"今日看点"})
    if not cur:
        abort(400)
    # kandians = [kandian for kandian in cur]
    #print list(cur)
    return json.dumps({
        "ret":0,
        "msg":"请求成功",
        "content":list(cur)}, default=json_util.default), 201, {'Content-Type': 'application/json'}
    # return jsonify({
    #     'content': json.dumps(kandians, default=json_util.default)})

@app.route('/content/zx/dy',methods=['GET'])
def get_zxdy():
    cur = dylol.find({"label":"最新电影"})
    if not cur:
        abort(400)
    # kandians = [kandian for kandian in cur]
    #print list(cur)
    return json.dumps({
        "ret":0,
        "msg":"请求成功",
        "content":list(cur)}, default=json_util.default), 201, {'Content-Type': 'application/json'}

@app.route('/content/zx/dsj',methods=['GET'])
def get_zxdsj():
    cur = dylol.find({"label":"最新电视剧"})
    if not cur:
        abort(400)
    # kandians = [kandian for kandian in cur]
    #print list(cur)
    return json.dumps({
        "ret":0,
        "msg":"请求成功",
        "content":list(cur)}, default=json_util.default), 201, {'Content-Type': 'application/json'}

@app.route('/content/zx/dm',methods=['GET'])
def get_zxdm():
    cur = dylol.find({"label":"最新动漫"})
    if not cur:
        abort(400)
    # kandians = [kandian for kandian in cur]
    #print list(cur)
    return json.dumps({
        "ret":0,
        "msg":"请求成功",
        "content":list(cur)}, default=json_util.default), 201, {'Content-Type': 'application/json'}

@app.route('/content/zx/zy',methods=['GET'])
def get_zxzy():
    cur = dylol.find({"label":"最新综艺"})
    if not cur:
        abort(400)
    # kandians = [kandian for kandian in cur]
    #print list(cur)
    return json.dumps({
        "ret":0,
        "msg":"请求成功",
        "content":list(cur)}, default=json_util.default), 201, {'Content-Type': 'application/json'}

@app.route('/content/zph/dy',methods=['GET'])
def get_zphdy():
    cur = dylol.find({"label":"周排行电影"})
    if not cur:
        abort(400)
    # kandians = [kandian for kandian in cur]
    #print list(cur)
    return json.dumps({
        "ret":0,
        "msg":"请求成功",
        "content":list(cur)}, default=json_util.default), 201, {'Content-Type': 'application/json'}

@app.route('/content/zph/dsj',methods=['GET'])
def get_zphdsj():
    cur = dylol.find({"label":"周排行电视剧"})
    if not cur:
        abort(400)
    # kandians = [kandian for kandian in cur]
    #print list(cur)
    return json.dumps({
        "ret":0,
        "msg":"请求成功",
        "content":list(cur)}, default=json_util.default), 201, {'Content-Type': 'application/json'}

@app.route('/content/zph/dm',methods=['GET'])
def get_zphdm():
    cur = dylol.find({"label":"周排行动漫"})
    if not cur:
        abort(400)
    # kandians = [kandian for kandian in cur]
    #print list(cur)
    return json.dumps({
        "ret":0,
        "msg":"请求成功",
        "content":list(cur)}, default=json_util.default), 201, {'Content-Type': 'application/json'}

@app.route('/content/zph/zy',methods=['GET'])
def get_zphzy():
    cur = dylol.find({"label":"周排行综艺"})
    if not cur:
        abort(400)
    # kandians = [kandian for kandian in cur]
    #print list(cur)
    return json.dumps({
        "ret":0,
        "msg":"请求成功",
        "content":list(cur)}, default=json_util.default), 201, {'Content-Type': 'application/json'}

@app.route('/content/search',methods=['POST'])
def search():
    if not request.json or not 'name' in request.json:
        abort(400)
    name = request.json['name']
    cur = dylol.find({"name":name})
    return json.dumps({
        "ret":0,
        "msg":"请求成功",
        "content":list(cur)}, default=json_util.default), 202, {'Content-Type': 'application/json'}

@app.errorhandler(404)
def not_found(error):
    return make_response(jsonify({'error': 'Not found'}), 404)

@app.errorhandler(400)
def request_error(errot):
    return make_response(jsonify({'error': 'Not found'}), 400)


if __name__ == '__main__':
  app.run(debug = True)